package cn.shanke.torrentmanagerserver.controller;

import cn.shanke.torrentmanagerserver.btclient.BtClient;
import cn.shanke.torrentmanagerserver.btclient.BtClients;
import cn.shanke.torrentmanagerserver.btclient.model.TmSessionInfo;
import cn.shanke.torrentmanagerserver.controller.common.ResponseModel;
import cn.shanke.torrentmanagerserver.model.Client;
import cn.shanke.torrentmanagerserver.service.ClientService;
import cn.shanke.torrentmanagerserver.util.StringUtil;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import nl.stil4m.transmission.rpc.RpcException;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/client")
public class ClientController {
    @Resource
    private ClientService modelService;

    @GetMapping("/list")
    public ResponseModel listModels() {
        return ResponseModel.success().data(modelService.list(null));
    }

    @GetMapping(value = "/find/{id}")
    public ResponseModel getModelById(@PathVariable("id")Long id) {
        return ResponseModel.success().data(modelService.getById(id));
    }

    @PostMapping("/save")
    public ResponseModel saveModel(@RequestBody Client model) {
        model.setVolumeHost(StringUtil.removeLastSlash(model.getVolumeHost()));
        model.setAddress(StringUtil.removeLastSlash(model.getAddress()));
        modelService.saveOrUpdate(model);
        BtClients.put(model);

        if (StringUtils.hasText(model.getVolumeHost())) {
            BtClient btClient = BtClients.get(model.getId() + "");
            try {
                TmSessionInfo info = btClient.getSessionInfo();
                if (info != null && !StringUtils.hasText(info.getDownloadDir())) {
                    modelService.update(
                            new UpdateWrapper<Client>().lambda()
                                    .set(Client::getVolumeHost, info.getDownloadDir())
                                    .eq(Client::getId, model.getId())
                    );
                }
            } catch (RpcException e) {
                System.err.println("获取更新下载器信息失败！");
            }
        }
        return ResponseModel.success().data(model.getId());
    }

    @GetMapping("/delete/{id}")
    public ResponseModel deleteModel(@PathVariable("id")Long id) {
        modelService.removeById(id);
        BtClients.remove(id+"");
        return ResponseModel.success();
    }

    @GetMapping("/options")
    public ResponseModel options() {
        return ResponseModel.success().data(modelService.list(null));
    }

    @GetMapping("/info/{id}")
    public ResponseModel info(@PathVariable("id")Long id) {
        BtClient btClient = BtClients.get(id + "");
        TmSessionInfo info = null;
        try {
            info = btClient.getSessionInfo();
        } catch (RpcException e) {
            e.printStackTrace();
            return ResponseModel.failure(e.getMessage());
        }
        return ResponseModel.success().data(info);
    }
}
